<template>
  <v-dialog
    show-full-screen
    :fixed-body-height="false"
    use-body-scrolling
    v-model="visible"
    title="危险源详情"
    @cancel="visible = false"
    @confirm="visible = false"
  >
    <div class="detail-content">
      <el-row :gutter="20">
        <template v-for="(value, key) in filteredData" :key="key">
          <el-col :span="24">
            <div class="detail-item">
              <span class="label">{{ formatLabel(key) }}：</span>
              <span class="value">
                <el-tag v-if="getMappedValue(key, value)" :type="getMappedValue(key, value).color">
                  {{ getMappedValue(key, value).text }}
                </el-tag>
                <span v-else>
                  {{ value || '无' }}
                </span>
              </span>
            </div>
          </el-col>
        </template>
      </el-row>
    </div>
  </v-dialog>
</template>
<script setup lang="ts">
import VDialog from "@/components/VDialog/VDialog.vue";
import { ref, computed } from "vue";
import { DangerSourcePageResponse } from "@/api/airport/safetyManage/dangerSource";
import { ElRow, ElCol } from "element-plus";

interface Props {
  modelValue: boolean;
  row: DangerSourcePageResponse;
}

const props = defineProps<Props>();
const emits = defineEmits<{
  (e: "update:modelValue", v: boolean): void;
}>();

const visible = computed({
  get: () => props.modelValue,
  set(v) {
    emits("update:modelValue", v);
  }
});

// 定义需要展示的字段
const fieldsToShow = [
  "dangerSourceId",
  "dangerSourceCode",
  "sourceType",
  "dangerSourceName",
  "dutyDeptName",
  "actionDeptName",
  "createBy",
  "professionalCategory",
  // "findTime",
  "dangerSourceOrigin",
  "dangerSourceMajorFlag",
  // "controlState",
  "dangerSourceDescribe",
  "harmDescribe",
  "emergeCause",
  "rootCause",
  "triggerFactor",
  "typicalEvent",
  "consequence",
  "havePreventMeasures",
  "verification",
  "remark",
  "riskBeforePossibility",
  "riskBeforeSeriousness",
  "riskBeforeVal",
  "riskBeforeRank",
  "riskAfterPossibility",
  "riskAfterSeriousness",
  "riskAfterVal",
  "riskAfterRank",
  "monitorDeptPeople",
  "monitorTrackSituation",
  "monitorFrequencyDate",
  "controlAfterPossibility",
  "controlAfterSeriousness",
  "controlAfterVal",
  "controlAfterRank",
  "derivedRiskExistFlag",
  "derivedRiskDescribe",
  "createTime",
  // "updateBy",
  // "updateTime",
  "status",
  // "relatedReformId",
  "relatedReformCode",
  "correctiveAction",
  "correctiveDeadline",
  "precautionAction",
  "precautionDeadline",
  "actionMakerName",
  "actionMakeTime",
  "riskCode",
  // "riskId",
  // "relatedQuestionId",
  // "associatedFlag",
  // "deriveDangerSourceId",
  "leaderContent",
  "leaderIsOk",
  "leaderName",
  "leaderTime",
  "reportTittle",
  "reformTittle",
  "drillPlanId",
  "hiddenTroubleRank",
  "rankJudgeName",
  "rankJudgeTime",
  // "hiddenTroubleType",
  "superviseDeptId",
  "rectificationTime",
  // "rectificationSchedule",
  "finishTime",
  "relatedRiskAction",
  "tempControlAction",
  "reformFunds",
  "actionIsOk",
  "actionEval",
  "actionEvalerName",
  "actionEvalTime"
];

// 过滤出需要展示且有值的字段
const filteredData = computed(() => {
  const data: Record<string, any> = {};

  fieldsToShow.forEach((field) => {
    if (props.row[field] !== undefined && props.row[field] !== null && props.row[field] !== '') {
      data[field] = props.row[field];
    }
  });
  return data;
});

// 格式化字段标签
const formatLabel = (key: string) => {
  const labelMap: Record<string, string> = {
    dangerSourceCode: "编号",
    // dangerType: "危险类型",
    sourceType:"类型",
    dangerSourceName: "危险源名称",
    dutyDeptName: "责任部门",
    actionDeptName: "措施责任部门",
    professionalCategory: "专业类别",
    // findTime: "发现时间",
    dangerSourceOrigin: "危险源来源",
    dangerSourceMajorFlag: "是否重大危险源",
    // controlState: "管控状态",
    dangerSourceDescribe: "危险源描述",
    harmDescribe: "危害描述",
    emergeCause: "诱发原因",
    rootCause: "根本原因",
    triggerFactor: "触发因素",
    typicalEvent: "典型事件",
    consequence: "后果",
    havePreventMeasures: "已有预防措施",
    verification: "验证情况",
    remark: "备注",
    riskBeforePossibility: "初始风险可能性",
    riskBeforeSeriousness: "初始风险严重性",
    riskBeforeVal: "初始风险值",
    riskBeforeRank: "初始风险等级",
    riskAfterPossibility: "现有风险可能性",
    riskAfterSeriousness: "现有风险严重性",
    riskAfterVal: "现有风险值",
    riskAfterRank: "现有风险等级",
    monitorDeptPeople: "监控部门及人员",
    monitorTrackSituation: "监控跟踪情况",
    monitorFrequencyDate: "监控频次",
    controlAfterPossibility: "管控后风险可能性",
    controlAfterSeriousness: "管控后风险严重性",
    controlAfterVal: "管控后风险值",
    controlAfterRank: "管控后风险等级",
    derivedRiskExistFlag: "是否存在衍生风险",
    derivedRiskDescribe: "衍生风险描述",
    createBy: "创建人",
    createTime: "创建时间",
    // updateBy: "更新人",
    // updateTime: "更新时间",
    status: "状态",
    relatedReformId: "关联整改ID",
    relatedReformCode: "关联整改编号",
    correctiveAction: "整改措施",
    correctiveDeadline: "整改期限",
    precautionAction: "预防措施",
    precautionDeadline: "预防措施期限",
    actionMakerName: "责任人",
    actionMakeTime: "责任制定时间",
    riskCode: "风险编号",
    riskId: "风险ID",
    relatedQuestionId: "关联问题ID",
    // associatedFlag: "是否关联",
    deriveDangerSourceId: "衍生危险源ID",
    leaderContent: "领导要求",
    leaderIsOk: "领导是否确认",
    leaderName: "领导姓名",
    leaderTime: "领导确认时间",
    reportTittle: "关联安全信息标题",
    reformTittle: "关联整改任务标题",
    drillPlanId: "演练计划ID",
    hiddenTroubleRank: "隐患等级",
    rankJudgeName: "等级判定人",
    rankJudgeTime: "等级判定时间",
    // hiddenTroubleType: "隐患类型",
    superviseDeptId: "督办部门ID",
    rectificationTime: "整改期限",
    // rectificationSchedule: "整改进度",
    finishTime: "完成时间",
    relatedRiskAction: "法律法规依据",
    tempControlAction: "临时管控措施",
    reformFunds: "整改资金",
    actionIsOk: "措施是否完成",
    actionEval: "措施评估",
    actionEvalerName: "评估人",
    actionEvalTime: "评估时间"
  };
  return labelMap[key] || key;
};

// 新增映射函数
const getMappedValue = (key: string, value: any) => {
  // 如果是需要映射的字段，返回映射后的值和颜色
  if (key === 'sourceType') {
    const sourceTypeMap: Record<string, { text: string; color: string }> = {
      '1': { text: '危险源', color: 'primary' },
      '2': { text: '隐患', color: 'warning' }
    };
    return sourceTypeMap[value] || null;
  }

  if (key === 'dangerSourceOrigin') {
    const originMap: Record<string, { text: string; color: string }> = {
      '1': { text: '风险识别', color: 'primary' },
      '2': { text: '隐患排查', color: 'success' },
      '3': { text: '安全质量检查', color: 'warning' },
      '4': { text: '安全专项检查', color: 'danger' },
      '5': { text: '法定自查', color: 'info' },
      '6': { text: '自愿报告', color: 'primary' },
      '7': { text: '安全预警', color: 'warning' },
      '8': { text: '应急案例', color: 'danger' },
      '9': { text: '通知公告', color: 'info' },
      '10': { text: '审核审计', color: 'success' },
      '11': { text: '外部检查', color: 'warning' }
    };
    return originMap[value] || null;
  }

  if (key === 'status') {
    const statusMap: Record<string, { text: string; color: string }> = {
      '1': { text: '风险评级中', color: 'primary' },
      '2': { text: '措施制定中', color: 'success' },
      '3': { text: '措施评级中', color: 'warning' },
      '4': { text: '整改中', color: 'danger' },
      '5': { text: '已关闭', color: 'info' }
    };
    return statusMap[value] || null;
  }

  if (key === 'actionIsOk' || key === 'leaderIsOk') {
    const yesNoMap: Record<string, { text: string; color: string }> = {
      '1': { text: '是', color: 'success' },
      '2': { text: '否', color: 'danger' }
    };
    return yesNoMap[value] || null;
  }

  // 其他字段直接返回 null
  return null;
};
</script>

<style scoped>
.detail-content {
  padding: 20px;
}

.detail-item {
  margin-bottom: 15px;
  display: flex;
  align-items: flex-start;
}

.label {
  width: 200px;
  font-weight: bold;
  flex-shrink: 0;
}

.value {
  flex: 1;
}
</style>
